package com.alibaba.fastjson.serializer.ext;

import java.io.IOException;
import java.util.Arrays;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonUtil {

	static SerializerFeature[] serializerFeature = { SerializerFeature.WriteDateUseDateFormat,
			SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.QuoteFieldNames,
			SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue,
			SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullNumberAsZero,
			SerializerFeature.WriteNullListAsEmpty };

	static SerializerFeature[] serializerFeature_ = { SerializerFeature.WriteDateUseDateFormat,
			SerializerFeature.BrowserCompatible, SerializerFeature.DisableCircularReferenceDetect,
			SerializerFeature.QuoteFieldNames, SerializerFeature.WriteNullStringAsEmpty,
			SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullBooleanAsFalse,
			SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty };

	/**
	 * 将对象转?��?JSON字符串�?并�?应�??�台
	 * 
	 * @param object
	 * @param includesProperties
	 *            ???转换?��???
	 * @param excludesProperties
	 *            不�?要转?��?属�?
	 */
	// public static void writeJsonByFilter(Object object,HttpServletRequest
	// request,HttpServletResponse response,MySerializeConfig cfg, String[]
	// includesProperties, String[] excludesProperties) {
	// try {
	// SimpleJsonFilter filter = new SimpleJsonFilter();// excludes优�?于includes
	// if (excludesProperties != null && excludesProperties.length > 0) {
	// filter.getExcludes().addAll(Arrays.<String> asList(excludesProperties));
	// }
	// if (includesProperties != null && includesProperties.length > 0) {
	// filter.getIncludes().addAll(Arrays.<String> asList(includesProperties));
	// }
	//
	// logger.info("对象转JSON：�??�除?��??�[" + excludesProperties + "]要�??��?属�?["
	// + includesProperties + "]");
	// String json;
	// String User_Agent = request.getHeader("User-Agent");
	// if (StringUtils.indexOfIgnoreCase(User_Agent, "MSIE 6") > -1) {
	// json = JSON.toJSONString(object,cfg,filter,serializerFeature);
	// } else {
	// json = JSON.toJSONString(object,cfg,filter,serializerFeature_);
	// }
	// response.setContentType("text/html;charset=utf-8");
	// response.getWriter().write(json);
	// response.getWriter().flush();
	// response.getWriter().close();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }

	public static String getJson(Object object, MySerializeConfig cfg) {
		String json;
		return json = JSON.toJSONString(object, cfg, serializerFeature_);
	}
}
